package org.travel_system.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.travel_system.mapper.ComplaintMapper;
import org.travel_system.pojo.Complaint;
import org.travel_system.service.ComplaintService;

import java.util.List;

@Service
public class ComplaintServiceImpl implements ComplaintService {

    private final ComplaintMapper complaintMapper;

    @Autowired
    public ComplaintServiceImpl(ComplaintMapper complaintMapper) {
        this.complaintMapper = complaintMapper;
    }
    @Override
    public Complaint getComplaintById(int id) {
        return complaintMapper.getComplaintById(id);
    }

    @Override
    public List<Complaint> getComplaints(Integer touristId) {
        return complaintMapper.getComplaints(touristId);
    }

    @Override
    public int addComplaint(Complaint complaint) {
        return complaintMapper.addComplaint(complaint);
    }

    @Override
    public int deleteComplaint(Complaint complaint) {
        return complaintMapper.deleteComplaint(complaint);
    }

    @Override
    public int updateComplaint(Complaint complaint) {
        return complaintMapper.updateComplaint(complaint);
    }

    @Override
    public String getFileTypeFromUrl(String url) {
        if (url == null || url.isEmpty()) {
            return null;
        }

        // Extract the part after the last '/'
        String fileName = url.substring(url.lastIndexOf('/') + 1);

        // Extract the file extension
        String fileExtension = "";
        int queryIndex = fileName.indexOf('?');
        if (queryIndex > 0) {
            fileName = fileName.substring(0, queryIndex);
        }

        int extensionIndex = fileName.lastIndexOf('.');
        if (extensionIndex > 0 && extensionIndex < fileName.length() - 1) {
            fileExtension = fileName.substring(extensionIndex + 1);
        }

        switch (fileExtension.toLowerCase()) {
            case "mp4":
                return "video";
            case "jpg":
            case "jpeg":
            case "png":
            case "gif":
            case "pdf":
                return "image";
            // Add more cases for other file types if needed
            default:
                return "unknown";
        }
    }

    @Override
    public List<Complaint> getAllComplaints() {
        return complaintMapper.getAllComplaints();
    }
}
